package com.fresh.liangyi.controller.client;

import com.fresh.liangyi.model.domain.Address;
import com.fresh.liangyi.model.domain.Goods;
import com.fresh.liangyi.model.domain.Order;
import com.fresh.liangyi.service.GoodsService;
import com.fresh.liangyi.service.OrderService;
import com.fresh.liangyi.service.UserService;
import com.fresh.liangyi.utils.UserUtils;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@Slf4j
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private UserService userService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrderService orderService;
    @RequestMapping("/toBuy")
    public String toBuy(Model model,
                        @RequestParam Integer[] goodsId){
        List<Goods> goodsList=new ArrayList<Goods>();
        log.info("goodsId:"+goodsId);
        for(int i=0;i<goodsId.length;i++){
            goodsList.add(goodsService.selectById(goodsId[i]));
        }
        model.addAttribute("goodsList",goodsList);
        List<Address> addressList=userService.getAddressByUserId(UserUtils.getUser().getId());
        model.addAttribute("addressList",addressList);
        return "client/buy";
    }

    @PostMapping("/buy")
    public String buy(Model model,
                      @RequestParam Integer[] goodsId,
                      @RequestParam Integer addressId){
        List<Goods> goodsList=new ArrayList<Goods>();
        for(int i=0;i<goodsId.length;i++){
            goodsList.add(goodsService.selectById(goodsId[i]));
            goodsService.deleteCart(UserUtils.getUser().getId(),goodsId[i]);
        }
        Address address=userService.getAddressById(addressId);
        Order order=new Order();
        order.setStatus(1);
        order.setCreateDate(new Date());
        order.setAddressId(addressId);
        order.setUserId(UserUtils.getUser().getId());
        double total_amount=0;
        for (Goods goods:goodsList) {
            total_amount+=goods.getPrice()*goods.getDiscount();
        }
        order.setTotalAmount(total_amount);
        //往数据库中提交订单
        orderService.orderAdd(order);
        //提交订单详情
        for (Goods goods:goodsList) {
            orderService.orderDetailsAdd(goods.getId(),order.getId());
        }
        //显示订单详情
        model.addAttribute("order",order);
        model.addAttribute("goodsList",goodsList);
        model.addAttribute("address",address);
        return "client/buyResult";
    }

    @GetMapping("/{orderId}")
    public String orderDetails(@PathVariable Integer orderId,
                               Model model){
        System.out.println("订单id"+orderId);
        List<Goods> goodsList=orderService.showGoodsByOrderId(orderId);
        Address address=orderService.getAddressByOrderId(orderId);
        Order order=orderService.selectOrderById(orderId);
        model.addAttribute("goodsList",goodsList);
        model.addAttribute("address",address);
        model.addAttribute("order",order);
        return "client/orderDetails";
    }
}
